<?php
class Core_View_Helper_SortBuilder extends Zend_View_Helper_Abstract{

    const PARAM_DEFAULT         = PARAM_SORT_DEFAULT;
    const COLUMN_DEFAULT        = COLUMN_DEFAULT;
	public function __construct()
	{
	}

    /**
     *
     * @param string $column
     * @return array
     */
    public function sortBuilder($column, $default_sort = 'DESC', array $options = null, $keepPrevious = false){
        $request			= Zend_Controller_Front::getInstance()->getRequest();
		$columnDefaultName	= self::COLUMN_DEFAULT;
		$valueDefault		= 'DESC';
		if( !empty($options['column_default']) )
		{
			$columnDefaultName	= $options['column_default'];
		}
		if( !empty($options['sort_value']) )
		{
			$valueDefault	= $options['sort_value'];
		}
        $params     = $request->getParam(self::PARAM_DEFAULT,array($columnDefaultName => $valueDefault));
		$arrSort	= array();
        if( isset($params[$column]) )
        {
            $params[$column] = (strcasecmp($params[$column],'DESC')===0)?'ASC':'DESC';
        }
        else
        {
            $params[$column] = $default_sort;
        }
        if( $keepPrevious == true)
        {
            $arrSort = array(self::PARAM_DEFAULT => $params);
        }
        else
        {
            $arrSort = array(self::PARAM_DEFAULT => array($column=>$params[$column]));
        }
		return $arrSort;
	}
}